package com.yoyo.ad.baidu;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.baidu.mobads.AdView;
import com.baidu.mobads.AdViewListener;
import com.baidu.mobads.InterstitialAd;
import com.baidu.mobads.InterstitialAdListener;
import com.yoyo.ad.api.YoYoConfig;
import com.yoyo.ad.confusion.AdManage;
import com.yoyo.ad.main.IAdBannerListener;
import com.yoyo.ad.main.IAdFactory_;
import com.yoyo.ad.main.IAdInteractionListener;
import com.yoyo.ad.main.IAdView;

import org.json.JSONObject;

public class BaiduAdFactory implements IAdFactory_ {

    private IAdView mAdView;
    private IAdBannerListener mAdBannerListener;
    private IAdInteractionListener mAdInteractionListener;
    private Context mContext;

    private SparseArray<Long> adIds = new SparseArray<>();
    private SparseBooleanArray isIpReports = new SparseBooleanArray();
    private InterstitialAd interAd;
    private final int adType = AdManage.BAIDU;

    public BaiduAdFactory(Context context) {
        mContext = context;
    }

    @Override
    public void setAdView(IAdView adView) {
        if (mAdView == null && adView != null)
            mAdView = adView;
    }

    @Override
    public void setAdBannerListener(IAdBannerListener adView) {
        if (mAdBannerListener == null && adView != null)
            mAdBannerListener = adView;
    }

    @Override
    public void setAdInteractionListener(IAdInteractionListener adInteractionListener) {
        if (mAdInteractionListener == null && adInteractionListener != null)
            mAdInteractionListener = adInteractionListener;
    }

    @Override
    public void setConfig(long adId, int adPositionId, boolean isIpReport) {
        adIds.put(adPositionId, adId);
        isIpReports.put(adPositionId, isIpReport);
    }

    @Override
    public void getYoyoAd(int position, int requestCode, int size, String adPlaceId, int type) {

    }

    @Override
    public void getExpressAd(int position, int requestCode, String adPlaceId, int size, int width) {

    }

    @Override
    public void getBanner(int position, final int requestCode, String adPlaceId, ViewGroup adsParent, int width, int height) {
        adsParent.setVisibility(View.GONE);
        adsParent.removeAllViews();
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        FrameLayout.LayoutParams rllp = new FrameLayout.LayoutParams(width, height);
        //重要：请填上你的代码位ID,否则无法请求 到广告
        AdView adView = new AdView(mContext, adPlaceId);
        //设置监听器
        adView.setListener(new AdViewListener() {
            @Override
            public void onAdReady(AdView adView) {
                Log.e(YoYoConfig.TAG, "adReady");
            }

            @Override
            public void onAdShow(JSONObject jsonObject) {
                mAdBannerListener.adShow(requestCode);
            }

            @Override
            public void onAdClick(JSONObject jsonObject) {
                if (mAdInteractionListener != null)
                    mAdInteractionListener.adClick(requestCode);
            }

            @Override
            public void onAdFailed(String s) {
                mAdBannerListener.adFail(requestCode, s);
            }

            @Override
            public void onAdSwitch() {

            }

            @Override
            public void onAdClose(JSONObject jsonObject) {
                if (mAdInteractionListener != null)
                    mAdInteractionListener.adDismissed(requestCode);
            }
        });
        adsParent.addView(adView, rllp);
        adsParent.setVisibility(View.VISIBLE);
        AdManage.showAdSuccess(mContext, adType, adIds.get(position, 0L),
                position, isIpReports.get(position, false));
    }

    @Override
    public void getInteraction(final int position, final int requestCode, String adPlaceId) {
        interAd = new InterstitialAd(mContext, adPlaceId);
        interAd.setListener(new InterstitialAdListener() {
            @Override
            public void onAdReady() {
                interAd.showAd((Activity) mContext);
                AdManage.showAdSuccess(mContext, adType, adIds.get(position, 0L),
                        position, isIpReports.get(position, false));
            }

            @Override
            public void onAdPresent() {
                if (mAdInteractionListener != null)
                    mAdInteractionListener.adShow(requestCode);
            }

            @Override
            public void onAdClick(InterstitialAd interstitialAd) {
                interstitialAd.destroy();
                if (mAdInteractionListener != null)
                    mAdInteractionListener.adClick(requestCode);
            }

            @Override
            public void onAdDismissed() {
                if (mAdInteractionListener != null)
                    mAdInteractionListener.adDismissed(requestCode);
            }

            @Override
            public void onAdFailed(String s) {
                if (mAdInteractionListener != null)
                    mAdInteractionListener.adFail(requestCode, s);
            }
        });
        interAd.loadAd();
    }

    @Override
    public void onDestroy() {

    }
}
